
using System.Linq;
using System.Web.Mvc;
using Ezio.Models;

namespace Ezio.Controllers
{
    public class BodegasController : Controller
    {
        DataProvider Provider = new DataProvider();
        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Agregar(Bodegas Bodega)
        {
            Provider.AddToBodegas(Bodega);
            return RedirectToAction("Index", new { Success = Provider.TrySaveChanges() });
        }
        public ActionResult Index(bool? Success)
        {
            string Message = Success.HasValue ? Success.Value ? "" : "Ha ocurrido un error al procesar su peticion" : "";
            
            ViewData["Bodegas"] = Provider.Bodegas.ToList();
            ViewData["Message"] = Message;
            return View("Index");
        }
        public ActionResult Editar(int Id)
        {
            ViewData["Bodega"] = Provider.Bodegas.Single(t => t.IdBodega == Id);
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public bool Update(Bodegas Bodega)
        {
            Bodegas myBodega= Provider.Bodegas.Single(t => t.IdBodega == Bodega.IdBodega);
            myBodega.Copy(Bodega);
            return Provider.TrySaveChanges();
        }

        public ActionResult Eliminar(int Id)
        {
            Provider.Bodegas.DeleteObject(Provider.Bodegas.Single(t => t.IdBodega == Id));
            return RedirectToAction("Index", new { Success = Provider.TrySaveChanges() }); ;
        }

        public ActionResult StockEnBodega(int IdBodega)
        {
            ViewData["Stocks"] = Provider.Stocks_View.Where(t => t.IdBodega == IdBodega).ToList();
            return View();
        }

        public ActionResult VerMovimientos(int IdBodega, string NombreInsumo)
        {
            ViewData["Movimientos"] = Provider.Movimientos_View.Where(t => t.IdBodega == IdBodega && t.NombreInsumo == NombreInsumo).ToList();
            return View();
        }
    }
}
